package it.desertsupport.utility;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import it.desertsupport.hibernate.sessionfactory.HibernateSessionFactory;
import it.desertsupport.model.Army;
import it.desertsupport.model.Defence;
import it.desertsupport.model.Profile;
import it.desertsupport.model.Role;
import it.desertsupport.model.User;
import it.desertsupport.hibernate.dao.BaseHibernateDAO;

import org.hibernate.Transaction;

public class TestSession extends BaseHibernateDAO {
	
	public static void main(String[] args) {
		TestSession testSession = new TestSession();
		testSession.setUp();
	}
	
	private void setUp() {
		
		Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
		
		Role roleAdmin = new Role(1l, "ADMIN", "administrator");
		getSession().save(roleAdmin);
		
		Role roleCommon = new Role(2l, "COMMON", "common user");
		getSession().save(roleCommon);
		
		Profile prifileAdmin = new Profile(1l, "Admin", "Admin", "rinino88@hotmail.com");
		getSession().save(prifileAdmin);
		
		User admin = new User(1l, "admin", md5hash("admin"), "YES", roleAdmin, prifileAdmin);
		getSession().save(admin);
		
		setUpArmy();
		setUpDefence();
		
		tx.commit();
		
	}
	
	private void setUpArmy(){
		   try
		   {
		     FileReader fr = new FileReader( "/home/gestdoc/Scrivania/Desert/supporto/armi.csv" );
		     BufferedReader br = new BufferedReader( fr );
		     
		     String stringRead = br.readLine( );
		     while( stringRead != null ){
		    	 
		       StringTokenizer st = new StringTokenizer( stringRead, ";" );
		       
		       Army army = new Army();
		       // set
		       army.setIdArmy(Long.parseLong(st.nextToken()));
		       army.setWeapon(st.nextToken());
		       army.setPrice(Long.parseLong(st.nextToken()));
		       army.setAttackPoints(Long.parseLong(st.nextToken()));
		       army.setDefencePoints(Long.parseLong(st.nextToken()));
		       army.setConstructionTime(st.nextToken());
		       army.setFuel(st.nextToken());
		       army.setFuelConsumption(Long.parseLong(st.nextToken()));
		       army.setAmmo(Long.parseLong(st.nextToken()));
		       army.setCost(Long.parseLong(st.nextToken()));
		       army.setTimeTravel(st.nextToken());
		       army.setSuperUnit(st.nextToken());
		       
		       getSession().save(army);
		       
		       stringRead = br.readLine( );
		     }
		 //add to output file
		     br.close( );
		   } 
		   catch( FileNotFoundException fnfe ) {
			   fnfe.printStackTrace();
		   } 
		   catch( IOException ioe ) {
		     ioe.printStackTrace( );
		   }

	}
	
	private void setUpDefence(){
		   try
		   {
		     FileReader fr = new FileReader( "/home/gestdoc/Scrivania/Desert/supporto/difesa.csv" );
		     BufferedReader br = new BufferedReader( fr );
		     
		     String stringRead = br.readLine( );
		     while( stringRead != null ){
		    	 
		       StringTokenizer st = new StringTokenizer( stringRead, ";" );
		       
		       Defence defence = new Defence();
		       // set
		       defence.setIdDefence(Long.parseLong(st.nextToken()));
		       defence.setWeapon(st.nextToken());
		       defence.setPrice(Long.parseLong(st.nextToken()));
		       defence.setDefencePoints(Long.parseLong(st.nextToken()));
		       defence.setConstructionTime(st.nextToken());
		       
		       getSession().save(defence);
		       
		       stringRead = br.readLine( );
		     }
		 //add to output file
		     br.close( );
		   } 
		   catch( FileNotFoundException fnfe ) {
			   fnfe.printStackTrace();
		   } 
		   catch( IOException ioe ) {
		     ioe.printStackTrace( );
		   }

	}
	
	/*
	 * metodo privato per criptare tramite l'MD5 la password.
	 * per ora è un metodo privato dell'implementazione del Service dell'Utente.
	 */
	private static String md5hash(String password) {
		String hashString = null;
		try {
			java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
			byte[] hash = digest.digest(password.getBytes());
			hashString = "";
			for (int i = 0; i < hash.length; i++) {
				hashString += Integer.toHexString( 
						(hash[i] & 0xFF) | 0x100 
	                              ).toLowerCase().substring(1,3);
			}
		} catch (java.security.NoSuchAlgorithmException e) {
			System.out.println(e);
		}
		return hashString;
	}
	
}
